|
1
|
|
|
/** global: io */ |
|
2
|
|
|
const socket = io.connect(); |
|
3
|
|
|
const streamId = window.location.pathname.substring(1); |
|
4
|
|
|
const autoScrool = true; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Gets element with log message data. |
|
8
|
|
|
* |
|
9
|
|
|
* @param {String} data Log message data. |
|
10
|
|
|
*/ |
|
11
|
|
|
function getCodeElement(data) { |
|
12
|
|
|
const c = document.createElement('code'); |
|
13
|
|
|
c.className = 'code blink hljs json'; |
|
14
|
|
|
c.innerHTML = JSON.stringify(data, null, "\t"); |
|
15
|
|
|
/** global: hljs */ |
|
16
|
|
|
hljs.highlightBlock(c); |
|
17
|
|
|
|
|
18
|
|
|
const pre = document.createElement('pre'); |
|
19
|
|
|
pre.appendChild(c); |
|
20
|
|
|
|
|
21
|
|
|
return pre; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Gets element with ip address. |
|
26
|
|
|
* |
|
27
|
|
|
* @param {String} ip Ip address. |
|
28
|
|
|
*/ |
|
29
|
|
|
function getIpElement(ip) { |
|
30
|
|
|
const s = document.createElement('span'); |
|
31
|
|
|
s.className = 'ip'; |
|
32
|
|
|
s.innerHTML = ip; |
|
33
|
|
|
|
|
34
|
|
|
return s; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Gets element with current date. |
|
39
|
|
|
*/ |
|
40
|
|
|
function getDateElement() { |
|
41
|
|
|
const s = document.createElement('span'); |
|
42
|
|
|
s.className = 'date'; |
|
43
|
|
|
s.innerHTML = (new Date()).toLocaleDateString( |
|
44
|
|
|
'en-GB', |
|
45
|
|
|
{ hour: '2-digit', minute: '2-digit', second: '2-digit' }, |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
return s; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Gets element with tags content. |
|
53
|
|
|
* |
|
54
|
|
|
* @param {Object} data LOG.NEW payload. |
|
55
|
|
|
*/ |
|
56
|
|
|
function getTags(data) { |
|
57
|
|
|
const d = document.createElement('div'); |
|
58
|
|
|
d.className = 'tags'; |
|
59
|
|
|
d.appendChild(getDateElement()); |
|
60
|
|
|
d.appendChild(getIpElement(data.ip)); |
|
61
|
|
|
|
|
62
|
|
|
return d; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Renders new log message. |
|
67
|
|
|
* |
|
68
|
|
|
* @param {Object} data LOG.NEW payload. |
|
69
|
|
|
*/ |
|
70
|
|
|
function renderJson(data) { |
|
71
|
|
|
const p = document.createElement('p'); |
|
72
|
|
|
p.appendChild(getTags(data)); |
|
73
|
|
|
p.appendChild(getCodeElement(data.data)); |
|
74
|
|
|
document.getElementById('root').appendChild(p); |
|
75
|
|
|
|
|
76
|
|
|
if (autoScrool === true) { |
|
77
|
|
|
window.scrollTo(0, document.body.scrollHeight); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Auto-scrolling to latest data. |
|
83
|
|
|
*/ |
|
84
|
|
|
window.onscroll = function () { |
|
85
|
|
|
autoScrool = ( |
|
86
|
|
|
(window.innerHeight + window.scrollY) >= document.body.offsetHeight |
|
87
|
|
|
); |
|
88
|
|
|
}; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Handler for new data. |
|
92
|
|
|
* Renders data on web page (add to the bottom). |
|
93
|
|
|
* |
|
94
|
|
|
* @event LOG.NEW |
|
95
|
|
|
*/ |
|
96
|
|
|
socket.on('log', function (data) { |
|
97
|
|
|
if (data.streamId === streamId) { |
|
98
|
|
|
if (data.format === 'json') { |
|
99
|
|
|
renderJson(data); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
}); |
|
103
|
|
|
|